package com.zw.yzk.kabu.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.constant.ActivityConstant;
import com.zw.yzk.kabu.model.MyFansModel;
import com.zw.yzk.kabu.view.activity.HomePageActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by zhanwei on 2017/2/11.
 */

public class FansWallAdapter extends RecyclerView.Adapter<FansWallAdapter.Holder> {

    private List<MyFansModel> dataList;
    private Context context;

    public FansWallAdapter(List<MyFansModel> list) {
        dataList = new ArrayList<>();
        if (list != null) {
            dataList.addAll(list);
        }
    }

    public List<MyFansModel> getDataList() {
        return dataList;
    }

    public void refresh(List<MyFansModel> list) {
        dataList.clear();
        if (list != null) {
            dataList.addAll(list);
        }
    }

    public void add(List<MyFansModel> list) {
        if (list != null) {
            int position = dataList.size();
            dataList.addAll(list);
            notifyItemRangeInserted(position, list.size());
        }
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (context == null) {
            context = parent.getContext();
        }
        View view = LayoutInflater.from(context).inflate(R.layout.recycler_item_fans_wall, parent, false);
        return new Holder(view);
    }

    @Override
    public void onBindViewHolder(final Holder holder, int position) {
        final MyFansModel model = dataList.get(position);
        holder.name.setText(model.getName());

        Glide.with(context)
                .load(model.getHead())
                .error(R.drawable.ic_default_head)
                .placeholder(R.drawable.ic_default_head)
                .centerCrop()
                .into(holder.head);

        holder.head.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, HomePageActivity.class);
                intent.putExtra(ActivityConstant.HOME_PAGE_ID, model.getId());
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    public static class Holder extends RecyclerView.ViewHolder {

        @Bind(R.id.head)
        ImageView head;
        @Bind(R.id.name)
        TextView name;

        public Holder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

}
